﻿using DevExpress.Persistent.BaseImpl;
using EasyXaf.EntityDesigner.BusinessObjects.Enums;

namespace EasyXaf.EntityDesigner.BusinessObjects.Entities.EntityProperties;

/// <summary>
/// 实体属性
/// </summary>
public partial class EntityProperty : BaseObject, ISortable
{
    private string GetEnumName()
    {
        if (EnumType != null)
        {
            return EnumType.Name;
        }
        else if (!string.IsNullOrWhiteSpace(EnumTypeFullName))
        {
            return EnumTypeFullName.Split('.').LastOrDefault();
        }
        return string.Empty;
    }

    private partial string GetDisplayName()
    {
        if (!string.IsNullOrWhiteSpace(Caption))
        {
            return $"{Name}({Caption})";
        }
        return Name;
    }

    private partial string GetTypeName()
    {
        var type = PropertyType switch
        {
            PropertyType.String => "string",
            PropertyType.LongString => "string",
            PropertyType.Integer => $"int",
            PropertyType.Long => $"long",
            PropertyType.Float => $"float",
            PropertyType.Double => $"double",
            PropertyType.Decimal => $"decimal",
            PropertyType.DateTime => $"DateTime",
            PropertyType.Guid => $"Guid",
            PropertyType.Boolean => $"bool",
            PropertyType.Enumeration => GetEnumName(),
            PropertyType.Binary => "byte[]",
            PropertyType.Image => "byte[]",
            PropertyType.File => "byte[]",
            PropertyType.Entity => EntityType?.Name,
            PropertyType.EntityCollection => EntityType?.Name,
            _ => "?",
        };

        if (PropertyType >= PropertyType.Integer && PropertyType <= PropertyType.Enumeration)
        {
            type = IsNullable == DefaultBoolean.True ? $"{type}?" : type;
        }

        return type;
    }

    private partial string GetTypeSummary()
    {
        var type = PropertyType switch
        {
            PropertyType.String => StringLength > 0 ? $"string({StringLength})" : "string",
            PropertyType.LongString => "string(max)",
            PropertyType.Integer => $"int",
            PropertyType.Long => $"long",
            PropertyType.Float => $"float",
            PropertyType.Double => $"double",
            PropertyType.Decimal => $"decimal",
            PropertyType.DateTime => $"DateTime",
            PropertyType.Guid => $"Guid",
            PropertyType.Boolean => $"bool",
            PropertyType.Enumeration => GetEnumName(),
            PropertyType.Binary => "byte[]",
            PropertyType.Image => "Image",
            PropertyType.File => "File",
            PropertyType.Entity => EntityType?.Name,
            PropertyType.EntityCollection => EntityType != null ? $"List<{EntityType.Name}>" : string.Empty,
            _ => null,
        };

        if (PropertyType == PropertyType.String)
        {
            if (IsNullable == DefaultBoolean.False)
            {
                if (StringLength > 0)
                {
                    type = $"string!({StringLength})";
                }
                else
                {
                    type = "string!";
                }
            }
        }
        else if (PropertyType == PropertyType.LongString)
        {
            if (IsNullable == DefaultBoolean.False)
            {
                type = "string!(max)";
            }
        }
        else if (PropertyType == PropertyType.Image || PropertyType == PropertyType.File)
        {
            if (IsNullable == DefaultBoolean.False)
            {
                type += "!";
            }
        }
        else if (PropertyType == PropertyType.Entity)
        {
            if (IsNullable == DefaultBoolean.False && !string.IsNullOrWhiteSpace(type))
            {
                type += "!";
            }
        }
        else if (IsNullable == DefaultBoolean.False && !string.IsNullOrWhiteSpace(type))
        {
            type += "?";
        }

        if (PropertyType >= PropertyType.Integer && PropertyType <= PropertyType.Enumeration)
        {
            type = IsNullable == DefaultBoolean.True ? $"{type}?" : type;
        }

        return string.IsNullOrWhiteSpace(type) ? "?" : type;
    }

    private partial string GetImageName()
    {
        if (IsReadonly)
        {
            return "Security_Lock";
        }
        else if (Entity.ValidationRules.Any(r => r.TargetProperty?.Name == Name))
        {
            return "Action_Validation_Validate";
        }
        return string.Empty;
    }

    private partial string GetPropertyTypeImage()
    {
        return PropertyType switch
        {
            PropertyType.String => "designer-input-text",
            PropertyType.LongString => "designer-textarea",
            PropertyType.Integer => "designer-input-number",
            PropertyType.Long => "designer-input-number",
            PropertyType.Float => "designer-input-number",
            PropertyType.Double => "designer-input-number",
            PropertyType.Decimal => "designer-input-number",
            PropertyType.DateTime => "designer-input-date",
            PropertyType.Boolean => "designer-checkboxes",
            PropertyType.Enumeration => "designer-enum",
            PropertyType.Guid => "designer-uuid",
            PropertyType.Binary => "designer-input-image",
            PropertyType.Image => "designer-input-image",
            PropertyType.File => "designer-input-file",
            PropertyType.Entity => "designer-form",
            PropertyType.EntityCollection => "designer-table-view",
            _ => "designer-help",
        };
    }

    private partial IList<Enumeration> GetEnumTypeDataSource()
    {
        return Entity.DomainModel.AllEnums;
    }

    private partial IList<Entity> GetEntityTypeDataSource()
    {
        return Entity.DomainModel.AllEntities;
    }
}
